package org.minuframework.json;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.minuframework.exception.ExceptionFailed;

/**
 * @Description Json.java
 * @author      minu
 * @date        2018/04/23 14:46:82
 */
public class JsonUtils {
    
    // ------------------------------------------------------------------------
    public static JSONArray toJSONArray(String jsonString) throws ExceptionFailed {
        JSONArray jsonArray = toJSONArraySafe(jsonString);
        
        if (null != jsonArray) { return jsonArray; }
        
        throw new ExceptionFailed("Cannot create JSONarray from " + jsonArray);
    }
    
    public static JSONArray toJSONArraySafe(String jsonString) {
        try {
            return new JSONArray(jsonString);
        } catch (Exception e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    public static JSONObject toJSONObject(String jsonString) throws ExceptionFailed {
        JSONObject jsonObject = toJSONObjectSafe(jsonString);
        
        if (null != jsonObject) { return jsonObject; }
        
        throw new ExceptionFailed("Cannot create JSONObject from " + jsonObject);
    }
    
    public static JSONObject toJSONObjectSafe(String jsonString) {
        try {
            return new JSONObject(jsonString);
        } catch (Exception e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    public static <T> void put(JSONObject target, String key, T value) throws ExceptionFailed {
        if (
            false == putSafe(target, key, value)
        ) { throw new ExceptionFailed("Cannot put " + key + " into " + target + ". Inner object is " + value); }
    }
    
    public static <T> boolean putSafe(JSONObject target, String key, T value) {
        try {
            target.put(key, value);
            return true;
        } catch (JSONException localJSONException) {}
        
        return false;
    }
    
    // ------------------------------------------------------------------------
    public static JSONArray getJsonArray(JSONObject jsonObject, String key) throws ExceptionFailed {
        JSONArray result = getJsonArraySafe(jsonObject, key);
        
        if (null == result) { throw new ExceptionFailed("Cannot get " + key + " from" + jsonObject); }
        
        return result;
    }
    
    public static JSONArray getJsonArraySafe(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONArray(key);
        } catch (JSONException e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    public static JSONObject getJsonObject(JSONObject jsonObject, String key) throws ExceptionFailed {
        JSONObject result = getJsonObjectSafe(jsonObject, key);
        
        if (null == result) { throw new ExceptionFailed("Cannot get " + key + " from" + jsonObject); }
        
        return result;
    }
    
    public static JSONObject getJsonObjectSafe(JSONObject jsonObject, String key) {
        try {
            return jsonObject.getJSONObject(key);
        } catch (JSONException e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    public static JSONObject getJsonObject(JSONArray jsonArray, int index) throws ExceptionFailed {
        JSONObject result = getJsonObjectSafe(jsonArray, index);
        
        if (null == result) { throw new ExceptionFailed("Cannot get JSONObject at index " + index + " from" + jsonArray); }
        
        return result;
    }
    
    public static JSONObject getJsonObjectSafe(JSONArray jsonArray, int index) {
        try {
            return jsonArray.getJSONObject(index);
        } catch (JSONException e) {}
        
        return null;
    }
    
    public static JSONArray getJsonArray(JSONArray jsonArray, int index) throws ExceptionFailed {
        try {
            return jsonArray.getJSONArray(index);
        } catch (JSONException e) {
            throw new ExceptionFailed("Cannot get JSONArray at index " + index + " from" + jsonArray);
        }
    }
    
    // ------------------------------------------------------------------------
    public static Object get(JSONArray jsonArray, int index) throws ExceptionFailed {
        try {
            return jsonArray.get(index);
        } catch (JSONException e) {
            throw new ExceptionFailed("Cannot get JSONArray at index " + index + " from" + jsonArray);
        }
    }
    
    public static Object getSafe(JSONArray jsonArray, int index) {
        try {
            return jsonArray.get(index);
        } catch (JSONException e) {}
        
        return null;
    }
    
    // ------------------------------------------------------------------------
    public static String prettyPoint(JSONObject jsonObject) {
        try {
            return jsonObject.toString(2);
        } catch (Exception e) {}
        
        return null;
    }
    
    public static String prettyPoint(String jsonString, String fallback) {
        JSONObject jsonObject = toJSONObjectSafe(jsonString);
        
        String formatted = prettyPoint(jsonObject);
        if (null == formatted) {
            formatted = fallback;
        }
        
        return formatted;
    }
    
}
